from celery import Celery
from celery_config import Config
from datetime import timedelta, datetime
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

celery = Celery('cloud_manager')

def load_config(role="worker"):
    config = Config(role=role)
    celery.config_from_object(config)
    logger.info(f"Celery broker_url: {celery.conf.broker_url}")
    logger.info(f"Celery result_backend: {celery.conf.result_backend}")

celery.autodiscover_tasks(['tasks'], force=True)

celery.conf.beat_schedule = {
    'check-vm-status-every-5-minutes': {
        'task': 'tasks.check_vm_status',
        'schedule': timedelta(minutes=5),
    },
    'clean-expired-tokens-every-hour': {
        'task': 'tasks.clean_expired_tokens',
        'schedule': timedelta(hours=1),
    },
    'generate-system-report-daily': {
        'task': 'tasks.generate_system_report',
        'schedule': timedelta(days=1),
    },
    'update-node-metrics-every-30-seconds': {
        'task': 'tasks.update_node_metrics',
        'schedule': timedelta(seconds=30),
        'options': {'eta': datetime.utcnow() + timedelta(seconds=60)}
    },
    'check-master-status-every-minute': {
        'task': 'tasks.check_master_status',
        'schedule': timedelta(minutes=1),
    },
    'update-iso-paths-every-minute': {
        'task': 'tasks.update_iso_paths',
        'schedule': timedelta(minutes=1),
    },
    'scan-and-init-vms-every-minute': {
        'task': 'tasks.scan_and_init_vms',
        'schedule': timedelta(minutes=1),
    },
    'check-network-connectivity-every-30-seconds': {
        'task': 'tasks.check_network_connectivity',
        'schedule': timedelta(seconds=30),
    },
}